"""
给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。

请你将两个数相加，并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外，这两个数都不会以 0 开头。
"""

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def addTwoNumbers( l1, l2):
    result = curr = ListNode(0)
    carry = 0
    while l1 or l2 or carry:
        val1 = l1.val if l1 else 0
        val2 = l2.val if l2 else 0
        total = val1 + val2 + carry
        carry = total//10

        curr.next = ListNode(total%10)
        curr = curr.next

        if l1:
            l1 = l1.next
        if l2:
            l2 = l2.next


    return result.next


def print1(l1):
    while l1:
        print(f"{l1.val} ",end="")
        l1 = l1.next
    print()
def creat_linklist(list):
    p = link_list = ListNode(0)
    for i in list:
        p.next = ListNode(i)
        p = p.next
    return link_list.next
l1 = creat_linklist([1,2,3,4,5,3,2,7,0])
l2 = creat_linklist([2,4,5,8,8,2,3,5,8,5])

print1(l1)
print1(l2)
print1(addTwoNumbers(l1,l2))
